package com.etc.traval.daoimpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.etc.traval.biz.SellerBiz;
import com.etc.traval.biz.UserBiz;
import com.etc.traval.bizimpl.SellerBizImpl;
import com.etc.traval.bizimpl.UserBizImpl;
import com.etc.traval.dao.RoomDao;
import com.etc.traval.dao.SellerDao;
import com.etc.traval.util.JDBCUtil;
import com.etc.travel.entity.Room;
import com.etc.travel.entity.Seller;

public class RoomDaoImpl implements RoomDao {
	JDBCUtil util = new JDBCUtil();
	SellerDao sdao = new SellerDaoImpl();

	/**
	 * 通过id查找房间
	 * 
	 * @author lwh
	 * @param 房间id
	 * @return 房间内容
	 */
	@Override
	public Room queryRoom(int id) {
		// TODO Auto-generated method stub
		ResultSet rs = util.query("select * from t_room where room_id=? ", id);
		Room room = null;
		try {
			if (rs.next()) {
				room = new Room();
				room.setRoom_id(rs.getInt("room_id"));
				room.setRoom_logo(rs.getString("room_logo"));
				room.setRoom_money(rs.getDouble("room_money"));
				room.setRoom_type(rs.getString("room_type"));
				room.setRoom_system(rs.getString("room_system"));
				room.setRoom_seller(sdao.selectSellerById(rs.getInt("room_seller")));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			util.closeAll();
		}

		return room;
	}

	/**
	 * 添加房间
	 * 
	 * @author lwh
	 * @param 房间
	 * @return 是否成功添加房间
	 */
	@Override
	public boolean addRoom(Room room) {
		// TODO Auto-generated method stub
		return util.update(
				"insert into t_room (room_money,room_type,room_logo,room_seller,room_system) values(?,?,?,?,?)",
				room.getRoom_money(), room.getRoom_type(), room.getRoom_logo(), room.getRoom_seller().getSeller_id(),
				room.getRoom_system()) > 0;
	}

	/**
	 * 删除房间
	 * 
	 * @author lwh
	 * @param 房间
	 * @return 是否成功删除
	 */
	@Override
	public boolean delRoom(int id) {
		return util.update("delete from t_room where room_id=?", id) > 0;
	}

	/**
	 * 通过房间id查找房间
	 * 
	 * @author lwh
	 * @param 房间id
	 * @return 房间内容
	 */
	@Override
	public List<Room> queryRoomBySeller(Seller seller) {
		ResultSet rs = util.query("select * from t_room where rowid in(select max(rowid) from t_room where room_seller=? group by room_type )", seller.getSeller_id());
		List<Room> list = new ArrayList<Room>();
		try {
			while (rs.next()) {
				Room room = new Room();
				room.setRoom_id(rs.getInt("room_id"));
				room.setRoom_logo(rs.getString("room_logo"));
				room.setRoom_money(rs.getDouble("room_money"));
				room.setRoom_type(rs.getString("room_type"));
				room.setRoom_system(rs.getString("room_system"));
				room.setRoom_seller(sdao.selectSellerById(rs.getInt("room_seller")));
				list.add(room);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			util.closeAll();
		}
		return list;
	}

	@Override
	public boolean updateRoomById(String system,int id) {
		return util.update("update t_room set room_system=? where room_id=?", system,id)>0;
	}

	@Override
	public List<Room> queryBySellerTypeSystem(Seller seller, String type, String system) {
		JDBCUtil util=new JDBCUtil();
		ResultSet rs = util.query("select * from T_ROOM where room_seller=? and room_type=?and  room_system=?",seller.getSeller_id(),type,system);
		List<Room> list=new ArrayList<Room>();
		SellerBiz biz1=new SellerBizImpl();
	
		try {
			while(rs.next()){
				Room room=new Room();
				room.setRoom_id(rs.getInt("room_id"));
				room.setRoom_logo(rs.getString("room_logo"));
				room.setRoom_money(rs.getDouble("room_money"));
				room.setRoom_system(rs.getString("room_system"));
				room.setRoom_type(rs.getString("room_type"));
				room.setRoom_seller(biz1.selectSellerById(rs.getInt("room_seller")) );
			    list.add(room);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			util.closeAll();
		}
		return list;
	}

	@Override
	public List<Room> queryAll(Seller seller) {
		JDBCUtil util=new JDBCUtil();
		ResultSet rs = util.query("select * from T_ROOM where room_seller=?",seller.getSeller_id());
		List<Room> list=new ArrayList<Room>();
		SellerBiz biz1=new SellerBizImpl();
	
		try {
			while(rs.next()){
				Room room=new Room();
				room.setRoom_id(rs.getInt("room_id"));
				room.setRoom_logo(rs.getString("room_logo"));
				room.setRoom_money(rs.getDouble("room_money"));
				room.setRoom_system(rs.getString("room_system"));
				room.setRoom_type(rs.getString("room_type"));
				room.setRoom_seller(biz1.selectSellerById(rs.getInt("room_seller")) );
			    list.add(room);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			util.closeAll();
		}
		return list;
	}

}
